package com.service.finanial.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.client.model.pojo.bank.ArElectronic;
import com.client.model.pojo.financial.AiruiFinancialUser;
import com.client.model.pojo.financial.FinancialItem;
import com.client.model.pojo.user.ArUser;
import com.client.model.system.FinancialUserVo;
import com.client.model.system.FinancialVo;
import com.client.model.system.LoginUserVo;
import com.client.model.vo.common.Result;
import com.client.model.vo.common.ResultCodeEnum;
import com.service.finanial.mapper.AiruiFinancialUserMapper;
import com.service.finanial.mapper.FinanciaVoMapper;
import com.service.finanial.mapper.FinancialUserVoMapper;
import com.service.finanial.service.ElectronicFeignService;
import com.service.finanial.service.FinancialItemService;
import com.service.finanial.mapper.FinancialItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
* @author liuwenju
* @description 针对表【financial_item】的数据库操作Service实现
* @createDate 2024-04-01 20:58:13
*/
@Service
public class FinancialItemServiceImpl extends ServiceImpl<FinancialItemMapper, FinancialItem>
    implements FinancialItemService{

    @Autowired
    private FinanciaVoMapper financiaVoMapper;
    @Autowired
    private AiruiFinancialUserMapper aiMapper;

    @Autowired
    private FinancialUserVoMapper financialUserVoMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private ElectronicFeignService electronicFeignService;


    @Override
    public List<FinancialVo> finAll() {
        return financiaVoMapper.list();
    }

    @Override
    public List<FinancialUserVo> myList(String token) {
        String userInfo = redisTemplate.opsForValue().get("user:login"+token);
        ArUser sysUser = JSON.parseObject(userInfo, ArUser.class);
        return  financialUserVoMapper.myList(String.valueOf(sysUser.getUid()));
    }

    @Override
    public Result sell(String token,Integer id,Integer remark, Integer riskLevel, Integer startMoney) {
        if(remark==null){
            return Result.build(null,202,"没有股份");
        }
        if(remark-riskLevel<0){
            return Result.build(null,202,"您的股份不够");
        }
        if(id==null){
            return Result.build(null,202,"不存在");
        }
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        System.out.println(id);
        AiruiFinancialUser airuiFinancialUser = aiMapper.selectOne(queryWrapper);
        airuiFinancialUser.setRemark(remark-riskLevel);
        System.out.println(airuiFinancialUser);
        if(airuiFinancialUser.getRemark()==0){
            airuiFinancialUser.setStatus(1);
        }
        aiMapper.updateById(airuiFinancialUser);


        ArElectronic arElectronic = electronicFeignService.arElectronic(token);
        arElectronic.setBalance(arElectronic.getBalance().add(new BigDecimal(startMoney*riskLevel)));

        System.err.println("11111111111111111111"+arElectronic);
        electronicFeignService.update(arElectronic);

        return Result.build(true,ResultCodeEnum.SUCCESS);
    }

}




